import axios from 'axios'
import router from '../router'
import { ElMessage } from 'element-plus'

// axios.defaults.baseURL = 'http://127.0.0.1:5050';
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';

// //  请求拦截器
// axios.interceptors.request.use((config) => {
//   console.log('请求拦截器1:>>', config)
//   if (sessionStorage.getItem('token')) {
//       // 在拦截器里去配置 token
//       config.headers.Authorization =
//           'Bearer token=' + sessionStorage.getItem('token')
//   }
//   return config
// })

// 响应拦截
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    var message;
    const code = parseInt(error.response && error.response.status);
    switch (code) {
      case 401:
        console.log(401);
        router.push({
          path: '/login'
        });
        break;
      case 403:
        message = '操作受限，请联系管理员处理';
        break;
      case 404:
        message = `请求的资源 ${error.config.url} 不存在`;
        break;
      case 504:
        message = '网络故障，请联系管理员处理';
        break;
      default:
        console.log(code);
        message = `请求资源 ${error.config.url} 失败`;
        
        break;
    }

    if (message) {
      error.messageHandle = ElMessage.error(message);
    }

    return Promise.reject(error);
  })


export default axios
